<?php

namespace app\controllers\device;

use app\common\base\CommonController;

class DeviceController extends CommonController
{
    /**
     * 绑定设备
     */
    public function actionBindDevice()
    {
        $this->rules = [
            [['deviceName'], 'required'],
            [['deviceName'], 'string'],
        ];

        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('device', 'DeviceService', 'bindDevice', $this->userInfo->id, $inputs['deviceName']);

        return [
            'success' => $info['success'],
        ];
    }

    /**
     * 解绑设备
     */
    public function actionUnbindDevice()
    {
        $this->rules = [
            [['deviceName'], 'required'],
            [['deviceName'], 'string'],
        ];

        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('device', 'DeviceService', 'unbindDevice', $this->userInfo->id, $inputs['deviceName']);

        return [
            'success' => $info['success'],
        ];
    }

    /**
     * 设置开关状态
     */
    public function actionSetDeviceProperty()
    {
        $this->rules = [
            [['value','productKey','deviceName'],'required'],
            [['productKey','deviceName'],'string'],
            ['value','bool'],
        ];

        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('device', 'DeviceService', 'setdeviceproperty', $inputs['value'],$inputs['productKey'], $inputs['deviceName']);

        return [
            'response' => $info['response'],
        ];
    }

    /**
     * 查询数据状态
     */
    public function actionQueryDeviceOriginalPropertyStatus()
    {
        $this->rules = [
            [['asc','pageSize','productKey','deviceName'],'required'],
            [['productKey','deviceName'],'string'],
            ['asc','bool'],//0为正序，1为倒序
            ['pageSize','integer'],//每页最多显示50条
        ];

        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('device', 'DeviceService', 'querydeviceoriginalpropertystatus', $inputs['asc'],$inputs['pageSize'],$inputs['productKey'], $inputs['deviceName']);

        return [
            'response' => $info['response'],
        ];
    }

}